package com.bicon.botu.mall.member.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.member.common.MemberConstants;
import com.bicon.botu.mall.member.dto.LogisticsCenterDto;
import com.bicon.botu.mall.member.entity.Cart;
import com.bicon.botu.mall.member.mapper.CartMapper;
import com.bicon.botu.mall.member.dto.CartDto;
import com.bicon.botu.mall.member.dto.ProductSkuDto;
import com.bicon.botu.mall.member.dto.ProductDto;
import com.bicon.botu.mall.member.service.ICartService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.scanner.Constant;

import java.util.List;

/**
 * 购物车服务实现类
 *
 * @author 张露晨
 * @date 2017-07-19
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<CartDto> selectProduct(String productInfoId) {
        return cartMapper.selectCart(productInfoId);
    }

    @Override
    public Integer selectProductNum(String productInfoId) {
        return cartMapper.selectProductNum(productInfoId);
    }

    @Override
    public ProductDto selectProductInfo(String productInfoId) {
        return cartMapper.selectProductInfo(productInfoId);
    }

    @Override
    public List<ProductSkuDto> selectCouponsByPath(List<String> pathList) {
        return cartMapper.selectCouponsByPath(pathList);
    }

    @Override
    public List<ProductSkuDto> selectProductSku(String productInfoId) {
        return cartMapper.selectProductSku(productInfoId);
    }

    @Override
    public LogisticsCenterDto selectLogisticsCenter(String productId) {
        return cartMapper.selectLogisticsCenter(productId);
    }

    @Override
    public String selectProductState(String productInfoId) {
        return cartMapper.selectProductState(productInfoId);
    }

    /**
     * 删除购物车
     *
     * @param productRepertoryId 多个id
     * @param userId 用户id
     * @return
     */
    @Override
    public Boolean deleteCart(List<String> productRepertoryId, String userId) {
        List<Cart> cartList = this.selectList(new EntityWrapper<Cart>().in("product_info_id", productRepertoryId).eq("user_id",userId));
        if (CollectionUtils.isEmpty(cartList)) {
            return true;
        } else {
            boolean result = this.delete(new EntityWrapper<Cart>().in("product_info_id", productRepertoryId).eq("user_id",userId));
            if (result) {
                return true;
            }
        }
        return false;
    }
}
